#ifndef CIISCommonH
#define CIISCommonH

#define MSG_TX_UNHANDLED         255
#define MSG_TX_IDENTIFY          0
#define MSG_TX_ANALOG            1
#define MSG_TX_LPR               2
#define MSG_TX_CALOFS            3
#define MSG_TX_CALGAIN           4
#define MSG_TX_HELO              5
#define MSG_TX_CAPABILITY        6
#define MSG_TX_EEDATA            7
#define MSG_TX_ADREGDATA         8
#define MSG_TX_IRRESULT          9
#define MSG_TX_LPREND            10
#define MSG_TX_SHUNTVALUE        11
#define MSG_TX_SAMPLE            12
#define MSG_TX_VERSION           13

#define MSG_TX_SAMPLEEXTCH       15
#define MSG_RX_UIQ_SAMPLEEXTCH   16

#define MSG_TX_SHUNTS			 19	

#include "DBTables.hpp"



const DefaultEventTabSize = 1000;
const DefaultEventLevel = 1;

const float One_mS = 1.0/( 24 * 60 * 60 * 1000 ); // Coonvert TTime to millisec.
const float OneSecond = 1.0/( 24 * 60 * 60 ); // Coonvert TTime to sec.
const float OneMinute = 1.0/( 24 * 60 ); // Coonvert TTime to minutes.
const float OneHour = 1.0/( 24 ); // Coonvert TTime to hours.
const float OneDay = 1.0; // Coonvert TTime to days.

const MessageHeaderLength = 7;  // Client / LServer interface
const OutMessageLimit = 12288;
const SocketInBufSize = 16384;
const SocketOutBufSize = 16384;
const SocketInputQueueSize = 32768;

// NetInterface
const UpdateAlarmDelay = 10000;
const CapabilityReqDelay = 3000;
const SetCANGroupDelay = 2000;
const IShuntReqDelay = 4000;
const VerReqDelay = 6000;
const RestartDelay = 12000;
const UpdatePSDelay = 100;
const SysClock = 100; // Timer interupt = 110 mS for Sampel clock. TTimer interval is a mul of 55mS
const SysClockDT = SysClock * One_mS;
const Sec_To_mS = 1000;  // Convert S to mS ( all timers are set in mS in NetInterface code )

enum CIIEventType { TypeUDef, SystemEvent, LevAlarm, ClientCom, CANCom };
enum CIIEventCode { CodeUDef,
										ETabCreated,
                    ServerStart,
                    ServerStop,
                    SensorLow,
                    SensorHigh,
                    SensorNormal,
                    PSLowU,
                    PSHighU,
                    PSLowI,
                    PSHighI,
                    PSNormalU,
                    PSNormalI,
                    LogIn,
										LogOut,
                    AlarmEnable,
                    AlarmDisable,
                    PSVOutEnable,
                    PSVOutDisable,
                    PSRemote,
                    PSLocal,
                    SensorMoved,
                    PSMoved,
                    PrjTabChanged,
                    CtrlTabChanged,
                    ZoneTabChanged,
                    SensorTabChanged,
                    PSTabChanged,
										RecStart,
										RecStop,
										ResetCan,
										RestartCan,
										TabProjectUpdated,
										TabControllersUpdated,
										TabZonesUpdated,
										TabNodesUpdated,
										TabSensorsUpdated,
										TabPowerSupplyUpdated,
										TabRecordingsUpdated,
										TabValuesUpdated,
										TabValuesLPRUpdated,
										TabValuesDecayUpdated,
										TabEventLogUpdated,
										LoggInError,
										TimeSet
									 };

enum CIIUserLevel { ULNone, ULReadOnly, ULModify, ULErase };

enum CIIEventLevel { LevelUDef, Low, Medium, High };

enum CIIMonitorState { MStart, PSOff, MSample, PSOn, MStop };

enum CIIServerStatus { CIISDisconnected, CIISConnected, CIISystemSync, CIIDataSync };

enum CIISServerMode { CIISNotInitiated, CIISLocalServer, CIISRemoteServer };

enum CIISCommand { CIISRead = 10, CIISWrite = 11, CIISAppend = 12,
									 CIISDelete = 13, CIISLogin = 20, CIISLogout = 21,
									 CIISSelectProject = 30, CIISExitProject = 31,
									 CIISReqStatus = 40, CIISReqDBVer = 41, CIISReqPrgVer = 42,
									 CIISReqTime = 43, CIISSetTime = 44};

enum CIISTable { CIISProject = 10, CIISController = 20, CIISSensors = 30,
                 CIISZones = 40, CIISValues = 50, CIISValuesLPR = 51,
                 CIISValuesDecay = 52, CIISRecordings = 55,
                 CIISPowerSupply = 60, CIISEvent = 70 };

enum CIISMsgType { CIISRequest = 10, CIISResponse = 20 };

enum CIIMsgCode { CIISMsg_Ok = 0, CIIMsg_SysSyncStarted = 1, CIISMsg_DataSyncStarted = 2,
									CIISMsg_Idle = 20, CIISMsg_Busy = 21,
									CIISMsg_RecNotFound = 110,
									CIISMsg_UnknownCommand = 111, CIISMsg_NoLongerSupported = 112, CIISMsg_LServerNotConn = 210,
									CIISMsg_LoggInError = 250, CIISMsg_UserLevelError = 251,
									CIISMsg_LoggInTimeOut = 240, CIISMsg_LoggOutTimeOut = 241,
									CIISMsg_ExitPrjTimeOut = 242, CIISMsg_CanSyncTimeOut = 243,
									CIISUnDefErr = 255 };

enum CIISCheckSyncState { CIISCheckSyncStart, CIISCheckSyncRequest,
                          CIISCheckSyncRead, CIISCheckSyncReady,
                          CIISCheckSyncError, CIISCheckSyncTimeout };

enum CIISLastValSyncState { CIISLastValSyncStart,
                            CIISLastValSyncSensorRequestFirst, CIISLastValSyncSensorRequest, CIISLastValSyncSensorRead,
                            CIISLastValSyncPSRequestFirst, CIISLastValSyncPSRequest, CIISLastValSyncPSRead,
                            CIISLastValSyncReady,
                            CIISLastValSyncError, CIISLastValSyncTimeout };

enum CIISDataSyncState { CIISDataSyncStart, CIISDataSyncValues,
                         CIISDataSyncLPR, CIISDataSyncDecay,
                         CIISDataSyncReady, CIISDataSyncError, CIISDataSyncTimeout };

enum CIISSysSyncState { CIISSysSyncStart,
                        CIISSysSyncCheck, CIISSysSyncLastVal,
                        CIISSysCheckProject,
                        CIISSysSyncEvent, CIISSysSyncProject,
                        CIISSysSyncCtrl, CIISSysSyncZone,
                        CIISSysSyncSensor, CIISSysSyncPS,
                        CIISSysSyncRec, CIISSysSyncReadyAfterCheck,
                        CIISSysSyncReady, CIISSysSyncError, CIISSysSyncTimeout };

enum CIISProjectSyncState{ CIISPrjSyncStart, CIISPrjSyncRequest, CIISPrjSyncReadRec,
                           CIISPrjSyncReady };                         


enum CIISEventSyncState { CIISEventSyncStart,
                          CIISEventSyncRequestFirst, CIISEventSyncRequestNext,
                          CIISEventSyncReadRec, CIISEventSyncReady };

enum CIISCtrlSyncState{ CIISCtrlSyncStart, CIISCtrlSyncRequest, CIISCtrlSyncReadRec,
                           CIISCtrlSyncReady };

enum CIISZoneSyncState{ CIISZoneSyncStart, CIISZoneSyncRequestFirst, CIISZoneSyncRequest,
                        CIISZoneSyncReadRec,CIISZoneSyncReady };

enum CIISSensorSyncState{ CIISSensSyncStart, CIISSensSyncRequestFirst, CIISSensSyncRequest,
                          CIISSensSyncReadRec, CIISSensSyncReady };

enum CIISPSSyncState{ CIISPSSyncStart, CIISPSSyncRequestFirst, CIISPSSyncRequest,
                      CIISPSSyncReadRec, CIISPSSyncReady };

enum CIISRecSyncState{ CIISRecSyncStart, CIISRecSyncRequestStopDate, CIISRecSyncReadStopDate,
                       CIISRecSyncRequestFirst, CIISRecSyncRequest,
                       CIISRecSyncReadRec, CIISRecSyncReady };

enum CIISValSyncState{ CIISValSyncStart, CIISValSyncRequestRecCount, CIISValSyncReadRecCount,
											 CIISValSyncRequestFirst, CIISValSyncRequest,
											 CIISValSyncReadRec, CIISValSyncReady };

enum CIISLPRSyncState{ CIISLPRSyncStart, CIISLPRSyncRequestRecCount,CIISLPRSyncReadRecCount,
											 CIISLPRSyncRequestFirst, CIISLPRSyncRequest,
											 CIISLPRSyncReadRec, CIISLPRSyncReady };

enum CIISDecaySyncState{ CIISDecaySyncStart, CIISDecaySyncRequestRecCount,CIISDecaySyncReadRecCount,
                         CIISDecaySyncRequestFirst, CIISDecaySyncRequest,
												 CIISDecaySyncReadRec, CIISDecaySyncReady };

enum CIISLoggInState{ CIISLoggInStart, CIISLoggInOpenCom, CIISLoggInRead, CIISLoggInTimeOut, CIISLoggInCancel };

enum CIISOpenComState{ CIISOpenCom, CIISOpenComCheck, CIISOpenComReady, CIISOpenComCancel };

enum CIISCloseComState{ CIISCloseCom, CIISCloseComCheck, CIISCloseComReady, CIISCloseComCancel };

enum CIISCancelSyncState{ CIISCancelSync, CIISCanselSyncWait, CIISCanselSyncClose };

enum CIISSelPrjState { CIISSelPrjLoggIn, CIISSelPrjCheckLoggIn, CIISSelPrjSystemSync, CIISSelPrjCheckSync,
											 CIISSelPrjConnClient, CIISSelPrjTimeOut, CIISSelPrjReady };

enum	CIISPMLoggInState { CIISPMLoggInCancelSync, CIISPMLoggInCheckCancelSync, CIISPMLoggInCheckCloseCom,
													CIISPMLoggInOk, CIISPMLoggInTimeOut, CIISPMLoggInReady };

enum	CIISPMLoggOutState { CIISPMLoggOutCancelSync, CIISPMLoggOutCheckCancelSync, CIISPMLoggOutCheckCloseCom,
                           CIISPMLoggOutOk, CIISPMLoggOutTimeOut, CIISPMLoggOutReady };

enum	CIISPMExitPrjState { CIISExitPrjCancelSync, CIISExitPrjCheckCancelSync, CIISExitPrjCheckCloseCom,
													 CIISPMExitPrjOk, CIISPMExitPrjTimeOut, CIISExitPrjReady };

enum	CIISMonLoggInState { CIISMonLoggInCancelSync, CIISMonLoggInCheckCancelSync, CIISMonLoggInCheckCloseCom,
													 CIISMonLoggIn, CIISMonCheckLoggIn, CIISMonSystemSync, CIISMonCheckSystemSync,
													 CIISMonLoggInOk, CIISMonLoggInTimeOut, CIISMonLoggInReady	};

enum	CIISMonLoggOutState {CIISMonLoggOutCancelSync, CIISMonLoggOutCheckCancelSync, CIISMonLoggOutCheckCloseCom,
   												 CIISMonLoggOutOk, CIISMonLoggOutTimeOut, CIISMonLoggOutReady	};

enum	CIISMonCancelSyncState {CIISMonCancelSync, CIISMonCheckCancelSync, CIISMonCancelSyncOk, CIISMonCanselSyncTimeOut,
															CIISMonCancelSyncReady };

//enum CIISLoggInState { CIISLoggInCancelSync, CIISLoggIn };



/*
enum CIIS___SyncState{ CIIS___SyncStart, CIIS___SyncRequest, CIIS___SyncReadRec,
                           CIIS___SyncReady };
*/
typedef void __fastcall ( __closure *ProcMessFP)(const Byte*);
typedef void __fastcall ( __closure *DebugStateFP)(int);
typedef void __fastcall ( __closure *MessInQueueFP)();


TDateTime __fastcall Now_ms_cleard();
TDateTime __fastcall Now_ms();
String __fastcall CIISFloatToStr( double d );
double __fastcall CIISStrToFloat( String s );
TDateTime __fastcall CIISStrToDateTime( String s );
String __fastcall CIISDateTimeToStr( TDateTime t );
bool __fastcall CIISSetSysTime( TDateTime t );
String __fastcall GetProgVersion();

__int64 __fastcall CIIDiskFree( String Path);
__int64 __fastcall CIITabFileSize( String DBDir, TTable *Tab );

template <class T> T max( T t1, T t2 )
{
	return ( t1 < t2 ) ? t2 : t1;
}

template <class T> T min( T t1, T t2 )
{
	return ( t1 > t2 ) ? t2 : t1;
}

//---------------------------------------------------------------------------

class TDataArg : public TCollectionItem
{
private:
        int FNum;
        String FVal;

protected:
public:
__published:
        __property int Num  = { read=FNum, write=FNum };
        __property String Val  = { read=FVal, write=FVal };

};

//---------------------------------------------------------------------------

class TCamurPacket : public TObject
{
private:
        Byte FMessageType;
        Byte FMessageNumber;
        Byte FMessageCode;
        Byte FMessageCommand;
        Byte FMessageTable;
        String FMessageData;
        TCollection *FArgs;
        int FPortNo;
        Byte FMessageOrgNumber;

protected:

public:
        __fastcall TCamurPacket();
        __fastcall ~TCamurPacket();
        String __fastcall GetArg(int n);
        String __fastcall GetDelArg( int n );
        bool __fastcall ArgInc(int n);

__published:
        __property Byte MessageType  = { read=FMessageType, write=FMessageType };
        __property Byte MessageNumber  = { read=FMessageNumber, write=FMessageNumber };
        __property Byte MessageCode  = { read=FMessageCode, write=FMessageCode };
        __property Byte MessageCommand  = { read=FMessageCommand, write=FMessageCommand };
        __property Byte MessageTable  = { read=FMessageTable, write=FMessageTable };
        __property String MessageData  = { read=FMessageData, write=FMessageData };
        __property TCollection *Args  = { read=FArgs, write=FArgs };
        __property int PortNo  = { read=FPortNo, write=FPortNo };
        __property Byte MessageOrgNumber  = { read=FMessageOrgNumber, write=FMessageOrgNumber };

};

typedef void __fastcall ( __closure *TransferReplyFP)( TCamurPacket* );

#endif

